package com.ss.android.article.base.feature.user.account;

import android.annotation.SuppressLint;
import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.article.base.app.AppData;
import com.ss.android.common.app.AbsApplication;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

public class RequestMobileService extends IntentService {
    final class a extends BroadcastReceiver {
        a(byte arg1) {
            this();
        }

        private a() {

        }

        public final void onReceive(Context arg3, Intent arg4) {
            if (!arg4.getBooleanExtra("noConnectivity", false) && (RequestMobileService.a())) {
                try {
                    arg3.startService(new Intent(arg3, RequestMobileService.class).setAction("action_internal"));
                } catch (Throwable v0) {
                }

                arg3.unregisterReceiver(((BroadcastReceiver) this));
                RequestMobileService.b();
            }
        }
    }

    final class b implements Runnable {
        b(byte arg1) {
            this();
        }

        private b() {

        }

        public final void run() {
            AbsApplication v0 = AbsApplication.getApplication();
            try {
                ((Context) v0).startService(new Intent(v0, RequestMobileService.class).setAction("action_internal"));
            } catch (Throwable v0_1) {
            }
        }
    }

    private static volatile boolean a;
    private static a b;
    private volatile int c;
    private volatile String d;
    private volatile int e;

    public RequestMobileService() {
        super(RequestMobileService.class.getName());
    }

    private void a(String arg8, boolean arg9) {
        if (!TextUtils.isEmpty(((CharSequence) arg8))) {
            try {
                if (!"0".equals(new JSONObject(arg8).optString("result"))) {
                    return;
                }

                this.a(false, null, arg8, arg9, true);
            } catch (Exception v2) {
                this.e = -2;
                this.a(true, v2, this.d, arg9, true);
            }
        }
    }

    private void a(boolean arg8, Exception arg9, String arg10, boolean arg11, boolean arg12) {
        String v2_1 = null;
        String v1_1;
        CharSequence v2 = null;
        int v6 = 10000;
        ArrayList v3 = new ArrayList();
        if (arg10 == null) {
            arg10 = "";
        }

        ((List) v3).add(new NameValueImpl("auth_data", arg10));
        ((List) v3).add(new NameValueImpl("get_mobile", "1"));
        String v4 = "force_mobile";
        String v0 = arg11 ? "1" : "0";
        ((List) v3).add(new NameValueImpl(v4, v0));
        v4 = "redirected";
        v0 = arg12 ? "1" : "0";
        ((List) v3).add(new NameValueImpl(v4, v0));
        if (arg8) {
            if (this.c > 0) {
                ((List) v3).add(new NameValueImpl("error_code", String.valueOf(this.c)));
            }

            if (TextUtils.isEmpty(this.d)) {
                this.d = Log.getStackTraceString(((Throwable) arg9));
            }

            if (!TextUtils.isEmpty(this.d)) {
                if (this.d.length() > v6) {
                    this.d = this.d.substring(0, v6);
                }

                ((List) v3).add(new NameValueImpl("error_text", this.d));
            }

            if (this.e >= 0) {
            } else {
                ((List) v3).add(new NameValueImpl("client_error", String.valueOf(this.e)));
            }
        }
        Object v0_1 = this.getSystemService(TELEPHONY_SERVICE);
        if (v0_1 != null) {
            try {
                v1_1 = com.bytedance.common.utility.a.b.b(((TelephonyManager) v0_1));
            } catch (Exception v1) {
                v1_1 = null;
            }

            if (!TextUtils.isEmpty(v1_1)) {
                ((List) v3).add(new NameValueImpl("mobile", AppLog.j(v1_1)));
            }

            v0 = ((TelephonyManager) v0_1).getNetworkOperatorName();
            if (TextUtils.isEmpty(((CharSequence) v0))) {
            } else {
                ((List) v3).add(new NameValueImpl("carrier", v0));
            }
        }
        int v0_2 = -1;
        try {
            v2_1 = NetworkUtils.executePost(v0_2, com.ss.android.account.aa.c, ((List) v3));
        } catch (Exception v0_3) {
            v0_2 = -1;
            try {
                v2_1 = NetworkUtils.executePost(v0_2, com.ss.android.account.aa.c, ((List) v3));
            } catch (Exception ex) {
            }
        }

        if (!TextUtils.isEmpty(((CharSequence) v2_1))) {
            try {
                v0 = new JSONObject(v2_1).getString("mobile");
                if (TextUtils.isEmpty(((CharSequence) v0))) {
                    return;
                }

                AppData v1_3 = AppData.getInstance();
                v1_3.mMobileByTelecom = v0;
                SharedPreferences.Editor v1_4 = v1_3.mContext.getSharedPreferences("app_setting", 0).edit();
                v1_4.putString("mobile_by_telecom", v0);
                SharedPreferenceCompat.commit(v1_4);
            } catch (Exception v0_3) {
            }
        }
    }

    static boolean a() {
        boolean v0;
        if (Build.VERSION.SDK_INT >= 21) {
            v0 = NetworkUtils.isNetworkAvailable(AbsApplication.getApplication());
        } else {
            if ((NetworkUtils.isNetworkAvailable(AbsApplication.getApplication())) && !NetworkUtils.isWifi(AbsApplication.getApplication())) {
                return true;
            }

            v0 = false;
        }

        return v0;
    }

    final String a(URLConnection arg10, boolean arg11, boolean arg12) {
        BufferedReader v7_1;
        String v1_2;
        BufferedReader v8;
        BufferedReader v2 = null;
        String v7 = null;
        this.c = 0;
        this.d = v7;
        try {
            this.c = ((HttpURLConnection) arg10).getResponseCode();
            v2 = new BufferedReader(new InputStreamReader(arg10.getInputStream()));
            StringBuilder v1_1 = new StringBuilder();
            while (true) {
                String v3_1 = v2.readLine();
                if (v3_1 == null) {
                    break;
                }
                v1_1.append(v3_1);
            }
            this.d = v1_1.toString();
            v1_2 = this.d;
            return v1_2;
        } catch (Exception v3) {
            int v1_3 = -1;
            this.e = v1_3;
            this.a(true, v3, null, arg11, arg12);
            return v7;
        } finally {
            if (v2 != null) {
                try {
                    v2.close();
                } catch (Exception e) {

                }
            }
        }
    }

    @SuppressLint(value = {"NewApi"})
    final void a(String arg11, Network arg12) throws Exception {
        String v0_1;
        boolean v4_1;
        String v3;
        JSONObject v0;
        Exception v6 = null;
        int v4 = -2;
        if (TextUtils.isEmpty(((CharSequence) arg11))) {
            return;
        }

        try {
            v0 = new JSONObject(arg11);
            if (v0.getInt("result") != 0) {
                if (!"302".equals(v0.optString("result"))) {
                    this.e = -3;
                    v3 = this.d;
                    v4_1 = arg12 != null ? true : false;
                    this.a(true, v6, v3, v4_1, false);
                    return;
                }

                try {
                    v0_1 = v0.getString("cmAdr");
                    if (arg12 != null) {
                        this.a(this.a(arg12.openConnection(new URL(v0_1)), true, true), true);
                        return;
                    }
                } catch (JSONException v2_1) {
                    this.e = v4;
                    v3 = this.d;
                    v4_1 = arg12 != null ? true : false;
                    this.a(true, ((Exception) v2_1), v3, v4_1, false);
                    return;
                }

                this.a(this.a(new URL(v0_1).openConnection(), false, true), false);
                return;
            }
        } catch (Exception v2) {
            this.e = v4;
            v3 = this.d;
            v4_1 = arg12 != null ? true : false;
            this.a(true, v2, v3, v4_1, false);
            return;
        }

        boolean v8 = arg12 != null ? true : false;
        this.a(false, v6, arg11, v8, false);
        return;

    }

    static a b() {
        RequestMobileService.b = null;
        return null;
    }

    protected void onHandleIntent(Intent arg12) {
        int v10 = -1;
        String v0 = arg12.getAction();
        if (!RequestMobileService.a || v0 != null) {
            RequestMobileService.a = true;
            if (!RequestMobileService.a() && !"action_internal".equals(v0)) {
                RequestMobileService.b = new a((byte) 0);
                AbsApplication.getApplication().registerReceiver(RequestMobileService.b, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
                return;
            }

            int v0_1 = -1;
            try {
                JSONObject v0_2 = new JSONObject(NetworkUtils.doGet(v0_1, com.ss.android.account.aa.b)).getJSONObject("data");
                String v2_1 = v0_2.optString("url");
                if (TextUtils.isEmpty(((CharSequence) v2_1))) {
                    v0_1 = v0_2.optInt("retry_delay");
                    if (v0_1 <= 0) {
                        return;
                    }

                    new Handler(Looper.getMainLooper()).postDelayed(new b((byte) 0), (((long) v0_1)) * 1000);
                    return;
                }

                if (!NetworkUtils.isNetworkAvailable(((Context) this))) {
                    return;
                }

                if (NetworkUtils.isWifi(((Context) this))) {
                    if (Build.VERSION.SDK_INT < 21) {
                        return;
                    }

                    Object v0_3 = this.getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkRequest.Builder v3 = new NetworkRequest.Builder();
                    v3.addCapability(12);
                    v3.addTransportType(0);
                    ((ConnectivityManager) v0_3).requestNetwork(
                            v3.build(),
                            new com.ss.android.article.base.feature.user.account.b(this, v2_1, ((ConnectivityManager) v0_3)));
                    return;
                }

                this.a(this.a(new URL(v2_1).openConnection(), false, false), null);
            } catch (Exception v2) {
                this.e = v10;
                this.a(true, v2, this.d, false, false);
            }
        }
    }
}

